#include "driver.h"
#include "CancelSafeQueue.h"

extern IO_CSQ g_CancelSafeQueue;
extern LIST_ENTRY g_TransportIrpQueue;
extern KSPIN_LOCK g_QueueLock;
extern BOOLEAN isBeginLog;

NTSTATUS US_ControlRoutine( IN PDEVICE_OBJECT fdo, IN PIRP pIrp)
{
	PIO_STACK_LOCATION IrpStack = NULL;
	PUSBFILTER_DEVICE_EXTENSION pdx;
	ULONG ControlCode; 
	PCHAR pWriteDataBuffer = NULL;
	
	IrpStack = IoGetCurrentIrpStackLocation(pIrp);
	ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
	
	switch(ControlCode)
	{
	case IOCTL_SET_TRANSPORT_PACKET: 

		IoCsqInsertIrp(&g_CancelSafeQueue, pIrp, NULL);
		return STATUS_PENDING;
		break;

	case IOCTL_BEGIN_LOG:
		isBeginLog  = TRUE;
		break;

	case  IOCTL_STOP_LOG:
		isBeginLog  = FALSE;
		break;

	case IOCTL_STOP_WATCH_DEVICE:
		pdx = (PUSBFILTER_DEVICE_EXTENSION) fdo->DeviceExtension;
		pdx->isWathDevice = FALSE;
		break;

	case IOCTL_WATCH_DEVICE:
		pdx = (PUSBFILTER_DEVICE_EXTENSION) fdo->DeviceExtension;
		pdx->isWathDevice  = TRUE;
		break;

	default:
		pIrp->IoStatus.Information  = 0;
		pIrp->IoStatus.Status       = STATUS_INVALID_DEVICE_REQUEST;
		IoCompleteRequest(pIrp, IO_NO_INCREMENT);
		return STATUS_INVALID_DEVICE_REQUEST;
	}
	
	pIrp->IoStatus.Information  = 0;
    pIrp->IoStatus.Status       = STATUS_SUCCESS;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
	return STATUS_SUCCESS;
}